/**
 * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bangyu.monitor.modular.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.alibaba.druid.support.json.JSONUtils;
import com.bangyu.monitor.core.common.annotion.BussinessLog;
import com.bangyu.monitor.core.common.annotion.Permission;
import com.bangyu.monitor.core.common.constant.Const;
import com.bangyu.monitor.core.common.constant.ConstantEnum;
import com.bangyu.monitor.core.common.constant.dictmap.BridgeDict;
import com.bangyu.monitor.core.common.constant.dictmap.DictMap;
import com.bangyu.monitor.core.common.constant.dictmap.UserDict;
import com.bangyu.monitor.core.common.constant.factory.ConstantFactory;
import com.bangyu.monitor.core.common.exception.BizExceptionEnum;
import com.bangyu.monitor.core.common.page.LayuiPageFactory;
import com.bangyu.monitor.core.log.LogObjectHolder;
import com.bangyu.monitor.core.shiro.ShiroKit;
import com.bangyu.monitor.core.util.BindingResultUtil;
import com.bangyu.monitor.modular.system.biz.BridgeBiz;
import com.bangyu.monitor.modular.system.entity.Bridge;
import com.bangyu.monitor.modular.system.entity.Role;
import com.bangyu.monitor.modular.system.entity.User;
import com.bangyu.monitor.modular.system.model.BridgeDto;
import com.bangyu.monitor.modular.system.model.DictDto;
import com.bangyu.monitor.modular.system.model.UserDto;
import com.bangyu.monitor.modular.system.service.BridgeService;
import com.bangyu.monitor.modular.system.service.DictService;
import com.bangyu.monitor.modular.system.warpper.BridgeWrapper;
import com.bangyu.monitor.modular.system.warpper.DictWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.BindingResultUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 字典控制器
 *
 * @author fengshuonan
 * @Date 2017年4月26日 12:55:31
 */
@Controller
@RequestMapping("/bridge")
@Slf4j
public class BridgeController extends BaseController {

    private String PREFIX = "/modular/system/bridge/";

    @Autowired
    private BridgeService bridgeService;

    @Autowired
    private BridgeBiz bridgeBiz;

    /**
     * 跳转到桥梁监控信息页面
     * @return
     */
    @RequestMapping("/monitor_info")
    public String monitorInfo() {
        //return PREFIX + "monitor_info.html";
        return   "/modular/system/view/monitorView.html";
    }


    /**
     * 跳转到桥梁监控信息页面
     * @return
     */
    @RequestMapping("/data_echarts")
    public String dataEcharts(@RequestParam("beamId") Integer beamId) {
        return PREFIX + "data_echarts.html";
    }

    /**
     * 跳转到桥梁数据统计页面
     * @return
     */
    @RequestMapping("/statistics_data")
    public String statisticsData(){
        return PREFIX + "statistics_tabs.html";
    }

    /**
     * 跳转到桥梁主页面
     * @return
     */
    @RequestMapping("/bridge")
    public String bridge() {
        return PREFIX + "bridge.html";
    }

    /**
     *
     * @return
     */
    @RequestMapping("/bridge_add")
    public String addView() {
        return PREFIX + "bridge_add.html";
    }

    /**
     * 新增桥梁
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:22 PM
     */
    @RequestMapping("/add")
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public ResponseData add(@Valid BridgeDto bridgeDto, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL.getCode(),"参数错误"+BindingResultUtil.fieldErrorLog(result));
        }
        this.bridgeService.addBridge(bridgeDto);
        return SUCCESS_TIP;
    }

    /**
     * 获取桥梁列表
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:22 PM
     */
    @RequestMapping("/page")
    @ResponseBody
    public Object page(@RequestParam(value = "condition",required = false) String condition) {
        Page<Bridge> page = bridgeBiz.pageByPermission(condition);

        Page<Map<String, Object>> warpper = new BridgeWrapper(page).wrap();
        return LayuiPageFactory.createPageInfo(warpper);
    }

    /**
     * 获取所有字桥梁数据
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:22 PM
     */
    @RequestMapping("/list")
    @ResponseBody
    public ResponseData list(@RequestParam(value = "condition",required = false) String condition) {
        List<Bridge> result = bridgeBiz.listByPermission(condition);
        return ResponseData.success(new BridgeWrapper(result).wrap());
    }

    /**
     * 删除字典记录
     *
     * @author fengshuonan
     * @Date 2018/12/23 5:22 PM
     */
    @BussinessLog(value = "删除桥梁", key = "bridgeId", dict = BridgeDict.class)
    @RequestMapping("/remove")
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public ResponseData delete(@RequestParam Long bridgeId) {

        //缓存被删除的名称
        this.bridgeService.deleteBridge(bridgeId);

        return SUCCESS_TIP;
    }

    /**
     * 跳转到修改角色
     *
     * @author fengshuonan
     * @Date 2018/12/23 6:31 PM
     */
    @Permission
    @RequestMapping("/bridge_edit")
    public String bridgeEdit(@RequestParam Long bridgeId) {
        System.out.println("into bridge_edit");
        if (ToolUtil.isEmpty(bridgeId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        Bridge bridge = this.bridgeService.getById(bridgeId);
        LogObjectHolder.me().set(bridge);
        return PREFIX + "bridge_edit.html";
    }


    /**
     *
     * @param bridgeDto
     * @param result
     * @return
     */
    @RequestMapping("/edit")
    @BussinessLog(value = "修改桥梁信息", key = "bridgeId", dict = BridgeDict.class)
    @ResponseBody
    @Permission(Const.ADMIN_NAME)
    public ResponseData edit(@Valid BridgeDto bridgeDto, BindingResult result) {
        if (result.hasErrors()) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL.getCode(),String.format("参数错误:%s",BindingResultUtil.fieldErrorLog(result)));
        }
        this.bridgeService.editBridge(bridgeDto);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/view/{bridgeId}")
    @ResponseBody
    public ResponseData view(@PathVariable Long bridgeId) {
        if (ToolUtil.isEmpty(bridgeId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        Bridge bridge = this.bridgeService.getById(bridgeId);
        Map<String, Object> bridgeMap = BeanUtil.beanToMap(bridge);

        return ResponseData.success(bridgeMap);
    }



}
